package com.fancyy.scm.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.fancyy.scm.R;

/**
 * 
 * Create custom Dialog windows for your application Custom dialogs rely on
 * custom layouts wich allow you to create and use your own look & feel.
 * 
 * Under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 * 
 * @author antoine vianey
 * 
 */
public class MyDialog extends Dialog {
	public MyDialog(Context context, int theme) {
		super(context, theme);
	}

	public MyDialog(Context context) {
		super(context);
	}

	/**
	 * Helper class for creating a custom dialog
	 */
	public static class Builder {

		private Context context;
		private String message;
		private String title;
		private String positiveButtonText;
		private String negativeButtonText;
		private View contentView;
		private View dialogView;
		private int textGravity = Gravity.CENTER;

		public View getDialogView() {
			return dialogView;
		}

		private OnClickListener positiveButtonClickListener, negativeButtonClickListener;

		public Builder(Context context) {
			this.context = context;
		}

		/**
		 * Set the Dialog message from String
		 *
		 * @param resource
		 * @return
		 */
		public Builder setTipsImageResource(int resource) {
			return this;
		}

		/**
		 * Set the Dialog message from String
		 *
		 * @param message
		 * @return
		 */
		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 *
		 * @param message
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		/**
		 * Set the Dialog title from resource
		 *
		 * @param title
		 * @return
		 */
		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder setMessageGravity(int gravity) {
			this.textGravity = gravity;
			return this;
		}

		/**
		 * Set a custom content view for the Dialog. If a message is set, the
		 * contentView is not added to the Dialog...
		 *
		 * @param v
		 * @return
		 */
		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		/**
		 * Set the positive button resource and it"s listener
		 *
		 * @param positiveButtonText
		 * @param listener
		 * @return
		 */
		public Builder setPositiveButton(int positiveButtonText, OnClickListener listener) {
			this.positiveButtonText = (String) context.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;
			return this;
		}

		/**
		 * Set the positive button text and it"s listener
		 *
		 * @param positiveButtonText
		 * @param listener
		 * @return
		 */
		public Builder setPositiveButton(String positiveButtonText, OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}

		/**
		 * Set the negative button resource and it"s listener
		 *
		 * @param negativeButtonText
		 * @param listener
		 * @return
		 */
		public Builder setNegativeButton(int negativeButtonText, OnClickListener listener) {
			this.negativeButtonText = (String) context.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}

		/**
		 * Set the negative button text and it"s listener
		 *
		 * @param negativeButtonText
		 * @param listener
		 * @return
		 */
		public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}
		
		/**
		 *  Show the dialog
		 */
		public MyDialog show() {
			MyDialog dialog = create();
			dialog.show();
			return dialog;
		}

		private int layoutID = R.layout.widget_dialog;
		public Builder setLayout(int layoutID) {
			this.layoutID = layoutID;
			return this;
		}



		/**
		 * Create the custom dialog
		 */
		public MyDialog create() {
			return createWithStyle(R.style.MyDialog);
		}

		public MyDialog createWithStyle(int style) {
			final MyDialog dialog = new MyDialog(context, style);
			// INIT DialogView
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            dialogView = inflater.inflate(layoutID, null);
            dialog.addContentView(dialogView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            
            // BUTTONS
            View negativeButton  = dialogView.findViewById(R.id.negativeButton);
            View positiveButton  = dialogView.findViewById(R.id.positiveButton);
            if (negativeButtonText == null || positiveButtonText == null) { // only one button
            	View line = dialogView.findViewById(R.id.btn_line);
				if (line != null) {
					line.setVisibility(View.GONE);
				}
				if (negativeButton != null) {
					negativeButton.setVisibility(View.GONE);
				}
            }
            
            // set the confirm button
            if (positiveButtonText != null) {
            	TextView text = (TextView) positiveButton.findViewById(R.id.positiveButtonTxt);
            	text.setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                	positiveButton.setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(
                                    		dialog, 
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            }
            // set the cancel button
            if (negativeButtonText != null) {
            	TextView text = (TextView) dialogView.findViewById(R.id.negativeButtonTxt);
            	text.setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                	negativeButton.setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                	negativeButtonClickListener.onClick(
                                    		dialog, 
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            }
            
            //
            // MESSAGE
            //
			if (message != null) {
				TextView msg = ((TextView) dialogView.findViewById(R.id.message));
				msg.setGravity(textGravity);
				msg.setMovementMethod(ScrollingMovementMethod.getInstance());
				msg.setText(message);
			}
			TextView msg = ((TextView) dialogView.findViewById(R.id.title));
			if (title != null) {
				msg.setVisibility(View.VISIBLE);
				msg.setText(title);
			} else {
            	msg.setVisibility(View.GONE);
			}
			if (contentView != null) {
				ViewGroup content = ((ViewGroup) dialogView.findViewById(R.id.content));
				content.removeAllViews();
				content.addView(contentView);
			}
            dialog.setContentView(dialogView);
            return dialog;
        }

	}
}